/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * SSLTemplate.java
 * netsystem
 * 2013年11月7日上午8:24:26
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.net.ftpserver;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

import org.apache.commons.net.ftp.FTPSClient;

public abstract class SSLTemplate extends ClientTemplate {
	 protected static final String FTPCLIENT_KEYSTORE = "client.jks";
	    protected static final String KEYSTORE_PASSWORD = "password";	 
	    protected KeyManager clientKeyManager;
	    protected TrustManager clientTrustManager;
	  
	    
	 
	    
	    protected boolean useImplicit() {
	        return false;
	    }

	    protected String getClientAuth() {
	        return "false";
	    }

	    public FTPSClient createFTPClient() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException  {
	        FTPSClient ftpsClient = new FTPSClient(useImplicit());
	        ClassLoader cl = Thread.currentThread().getContextClassLoader();
	        InputStream fin =  cl.getResourceAsStream(FTPCLIENT_KEYSTORE);	       
	        KeyStore store = KeyStore.getInstance("jks");
	        store.load(fin, KEYSTORE_PASSWORD.toCharArray());
	        fin.close();

	        // initialize key manager factory
	        KeyManagerFactory keyManagerFactory = KeyManagerFactory
	                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
	        keyManagerFactory.init(store, KEYSTORE_PASSWORD.toCharArray());

	        // initialize trust manager factory
	        TrustManagerFactory trustManagerFactory = TrustManagerFactory
	                .getInstance(TrustManagerFactory.getDefaultAlgorithm());

	        trustManagerFactory.init(store);
	        
	        clientKeyManager = keyManagerFactory.getKeyManagers()[0];
	        clientTrustManager = trustManagerFactory.getTrustManagers()[0];
	        
	        ftpsClient.setKeyManager(clientKeyManager);
	        ftpsClient.setTrustManager(clientTrustManager);

	        String auth = getAuthValue();
	        if (auth != null) {
	            ftpsClient.setAuthValue(auth);
	            
	            if(auth.equals("SSL")) {
	                ftpsClient.setEnabledProtocols(new String[]{"SSLv3"});
	            }
	        }
	        return ftpsClient;
	    } 
	    protected abstract String getAuthValue();
	 
}
